<?php 
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;

class Index extends AdminBase{
    protected function _initialize()
    {
        parent::_initialize();
        //站点信息
        $web = db('system')->where(['name'=>'site_config'])->value('value');
        $this->assign('web',unserialize($web));
    }
	public function index(){
		return view('index');
	}
	/*站点动态展示*/
	public function webSite()
    {
        $version = Db::query('SELECT VERSION() AS ver');
        $config = [
            'url'               => $_SERVER['HTTP_HOST'],
            'mysql_version'     => $version[0]['ver'],
            'max_upload_size'   => ini_get('upload_max_filesize')
        ];
        return view('config', ['config' => $config]);
    }
    /*站点配置*/
    public function siteConfig(){
        $system = Db::name('system')->select();
        foreach($system as $key=>$val){
            if(is_serialized($val['value'])){
                $system_value = unserialize($val['value']);
            }else{
                $system_value = $val['value'];
            }
            $this->assign($val['name'],$system_value);
        }
        return $this->fetch('site_config');
    }
    // 更新配置、
    public function updateConfig(){
        if (request()->isPost()) {
            $config_type = $this->request->post('config_type');
            switch ($config_type){
                case 'site_config': //站点配置
                    $site_config                = input('site_config/a');
                    //$site_config['site_tongji'] = htmlspecialchars_decode($site_config['site_tongji']);//反转转义字符
                    $data['value']              = serialize($site_config);
                    break;
                case 'email_config': //邮箱配置
                    $email_config                = input('email_config/a');
                    $data['value']              = serialize($email_config);
                    break;
                /*case 'system_config': //系统项配置
                    $system_config                = input('system_config/a');
                    $data['value']                = serialize($system_config);
                    foreach ($system_config as $key=>$val){
                        $system_config[$key] = $val=='true'?true:false;
                    }
                    //itkee_set_dynamic_config($system_config); //写入配置文件
                    break;*/
                /*case 'qq_config':   //QQ登录
                    $qq_config                   = input('qq_config/a');
                    $data['value']               = serialize($qq_config);
                    break;*/
            }
            if(Db::name('system')->where('name', $config_type)->find()){
                if (Db::name('system')->where('name', $config_type)->update($data) !== false) {
                    $this->success('提交成功');
                }else {
                    $this->error('提交失败');
                }
            }else {
                $data['name'] = $config_type;
                if (Db::name('system')->insert($data) !== false) {
                    $this->success('提交成功');
                }else {
                    $this->error('提交失败');
                }
            }
        }
    }
    /*修改密码*/
    public function changePassword(){
    	return view('change_password');
    }
    // 更新密码
    public function updatePassword(){
        if (request()->isPost()) {
            $admin_id    = session('admin_id');
            $data   = input('post.');
            if($data['password'] != $data['confirm_password']) $this->error('两次密码输入不一致');
            $result = Db::name('admin_user')->find($admin_id);
            if (password_verify($data['old_password'], $result['password'])) {
                $new_password = password_hash($data['password'], PASSWORD_DEFAULT);
                $res          = Db::name('admin_user')->where(['id' => $admin_id])->setField('password', $new_password);
                if ($res !== false) {
                    $this->success('修改成功','hslogin/logout');
                } else {
                    $this->error('修改失败');
                }
            } else {
                $this->error('原密码不正确');
            }
        }
    }
}